import { createSlice } from "@reduxjs/toolkit";
import axios from "axios";

const userInfoStore = createSlice({
  name: "userInfo",
  initialState: {
    userInfo: {},
    token: "",
  },
  reducers: {
    SET_TOKEN(state, action) {
        state.token = action.payload;
      },
    SET_USERINFO(state, action) {
      state.userInfo = action.payload;
    },
  },
});
const { SET_TOKEN,SET_USERINFO } = userInfoStore.actions;

const login = () => {
  return async (dispatch) => {
    const result = await axios({
      url: "http://192.168.100.81:48801/api/dobo-auth/token",
      method: "post",
      headers: {
        "Dobo-Auth": "bearer 1863821831140544514",
      },
      data: {
        account: "B1001",
        password: "123456",
        grantType: "password",
        language: "1",
      },
    });
    dispatch(SET_TOKEN(result.data.data.accessToken))
    dispatch(SET_USERINFO(result.data.data))
  };
};
const reducer = userInfoStore.reducer;

export { login };
export default reducer;
